package com.base.utils;


import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class DigestUtil {
	/**
	 * MD5，SHA
	 */
	public static final String MD5 = "MD5";
	public static final String SHA = "SHA";
	public static final String UTF8 = "UTF-8";

	public static final String appkey = "1ff5438d1c6f735967d681ac023c6594";
	public static final String appsecert = "e0923685565b88cca0805d11ed0b8ce1";

	public static  String digestMD5(Map<String, String> dataMap,String appSecret) {
		return digest(dataMap, appSecret, MD5);
	}

	public static  String digestSHA(Map<String, String> dataMap,String appSecret) {
		return digest(dataMap, appSecret, SHA);
	}

	private static  String digest(Map<String, String> dataMap,String appSecret, String degestType) {

		if (dataMap == null) {
			throw new IllegalArgumentException("数据不能为空");
		}
		if (appSecret == null) {
			throw new IllegalArgumentException("安全校验码数据不能为空");
		}
		if (degestType == null) {
			throw new IllegalArgumentException("摘要算法不能为空");
		}

		// 第一步：把字典按Key的字母顺序升序排序
		Map<String, String> headMap = new TreeMap<String, String>(dataMap);

		// 第二步：把所有参数名和参数值串在一起

		String str = serialMapToString(appSecret, headMap);
		// 第三步：使用MD5/SHA加密

		try {
			if (degestType.equals(MD5)) {
				return DigestUtils.md5Hex(str.getBytes(UTF8));
			} else {
				return DigestUtils.sha256Hex(str.getBytes(UTF8));
			}

		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();
			throw new RuntimeException("签名失败", e);
		}

		// 第四步：把二进制转化为大写的十六进制
	}

	/**
	 * 把所有参数名和参数值串在一起
	 * 
	 * @param headMap
	 * @return
	 */
	private static String serialMapToString(String appSecret,
			Map<String, String> headMap) {
		StringBuffer query = new StringBuffer(appSecret);

		Set<Entry<String, String>> paramSet = headMap.entrySet();

		for (Entry<String, String> param : paramSet) {

			query.append(param.getKey()).append(param.getValue());
		}

		query.append(appSecret);

		return query.toString();
	}

	/**
	 * 生成随机值 包含A-Z a-z 0-9
	 * 
	 * @param length
	 * @return
	 */
	public static String randomSecret(int length) {
		char[] ss = new char[length];
		int i = 0;
		while (i < length) {
			int f = (int) (Math.random() * 3);
			if (f == 0)
				ss[i] = (char) ('A' + Math.random() * 26);
			else if (f == 1)
				ss[i] = (char) ('a' + Math.random() * 26);
			else
				ss[i] = (char) ('0' + Math.random() * 10);
			i++;
		}
		String is = new String(ss);
		return is;
	}

	public String digestMD5(String unDecodeParam, String appsecert) {
		// TODO Auto-generated method stub
		String result ="";
		String md5Str = appsecert + unDecodeParam + appsecert;
		
		try{
			result = DigestUtils.md5Hex(md5Str.getBytes(UTF8));
		}catch(Exception e){
			e.printStackTrace();
			try {
				throw new Exception("验签失败",e);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		return result;
	}
	
	/**
	 * SHA256加密
	 * */
	public static String digestSHA256(String unSignStr, String appsecert) {
		// TODO Auto-generated method stub
		String result ="";
		String md5Str = appsecert + unSignStr + appsecert;
		LogUtil.log_e("HX", "未加密数据====>"+md5Str);
		try{
			result = new String(Hex.encodeHex(DigestUtils.sha256(md5Str.getBytes(UTF8))));
//			result = DigestUtils.sha256Hex(md5Str.getBytes(UTF8));
		}catch(Exception e){
			e.printStackTrace();
			try {
				throw new Exception("验签失败",e);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		LogUtil.log_e("HX", "加密数据====>"+result);
		return result;
	}

}
